﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace btnet.pages
{
    public partial class query : CommonPage
    {
        protected string exception_message;

        protected void Page_Load(object sender, EventArgs e)
        {
            Bug_Util.do_not_cache(Response);
            // If there is a users table, then authenticate this page
            try
            {
                DbUtil.execute_nonquery("select count(1) from users");
                security = new Security();
                security.check_security(HttpContext.Current, Security.MUST_BE_ADMIN);
            }
            catch (Exception)
            {
            }

            titl.Text = Bug_Util.get_setting("AppTitle", "BugTracker.NET") + " - "
                + "run query";


            if (IsPostBack)
            {
                if (queryarea.Value != "")
                {
                    try
                    {
                        ds = DbUtil.get_dataset(Server.HtmlDecode(queryarea.Value));
                    }
                    catch (Exception e2)
                    {
                        exception_message = e2.Message;
                        //exception_message = e2.ToString();  // uncomment this if you need more error info.
                    }
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        protected void displaylist()
        {
            Response.Write("<span class=err>" + exception_message + "</span><br>");

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                SortableHtmlTable.create_from_dataset(
                    Response, ds, "", "");
            }
            else
            {
                Response.Write("No Rows");
            }
        }
    }
}
